﻿using Controller.Enum;
using Controller.Utils;
using Leeder.Logger;
using System;
using System.Collections.Generic;

namespace Controller.Model
{
    /// <summary>
    /// DPS通讯服务的数据包模型
    /// </summary>
    public class ServerPacketModel
    {
        protected LogOperate logObj => Leeder.Logger.Log.GetLogger(typeof(ServerPacketModel));

        /// <summary>
        /// 数据包帧头
        /// </summary>
        public const byte HEAT = 0x8D;
        /// <summary>
        /// 数据包帧尾
        /// </summary>
        public const byte TAIL = 0x8F;
        /// <summary>
        /// 数据报文基础长度
        /// </summary>
        public const byte PACKET_BASE_LENGTH = 6;
        /// <summary>
        /// 数据报文帧尾长度
        /// </summary>
        public const byte PACKET_TAIL_LENGTH = 1;
        /// <summary>
        /// 数据报文帧头长度
        /// </summary>
        public const byte PACKET_HEAT_LENGTH = 1;
        /// <summary>
        /// 数据报文命令类型的起始位置
        /// </summary>
        public const byte PACKET_CMD_TYPE_START_POSITION = PACKET_HEAT_LENGTH;
        /// <summary>
        /// 数据报文命令类型的长度
        /// </summary>
        public const byte PACKET_CMD_TYPE_LENGTH = 2;
        /// <summary>
        /// 数据报文数据长度的起始位置
        /// </summary>
        public const byte PACKET_BODY_LENGTH_START_POSITION = PACKET_HEAT_LENGTH + PACKET_CMD_TYPE_LENGTH;

        /// <summary>
        /// 命令类型
        /// </summary>
        public CommunicationCmdTypeEnum CmdType { get; set; }
        /// <summary>
        /// 命令体长度
        /// </summary>
        public UInt16 BodyLength { get; set; }
        ///// <summary>
        ///// 控制器的IP地址
        ///// </summary>
        //public string IpAddress { get; set; }
        ///// <summary>
        ///// 控制器的端口号
        ///// </summary>
        //public int Port { get; set; }
        /// <summary>
        /// 控制器键
        /// </summary>
        public string ControllerKey { get; set; }

        public override string ToString()
        {
            return $"类型:{CmdType}";
        }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public virtual byte[] BodyData()
        {
            return null;
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        public virtual void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            return;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns></returns>
        public byte[] Serialize()
        {
            List<byte> result = new List<byte> { HEAT };
            result.AddRange(BitConverter.GetBytes((UInt16)CmdType));
            byte[] tmp = BodyData();
            BodyLength = (ushort)(tmp == null ? 0 : tmp.Length);
            result.AddRange(BitConverter.GetBytes(BodyLength));
            if (0 != BodyLength)
            {
                result.AddRange(tmp);
            }
            result.Add(TAIL);
            return result.ToArray();
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <returns></returns>
        public void UnSerialize(byte[] iData)
        {
            int position = 1;
            CmdType = (CommunicationCmdTypeEnum)BitConverter.ToUInt16(iData, position);
            position += sizeof(UInt16);
            BodyLength = BitConverter.ToUInt16(iData, position);
            position += sizeof(UInt16);
            ParseBodyData(iData, position);
        }

    }

    /// <summary>
    /// 取消数据包模型,Client->Service
    /// </summary>
    public class CancelPacketModel : ServerPacketModel
    {
        /// <summary>
        /// 设备厂家
        /// </summary>
        public EquipmentBrandEnum EquipmentBrand { get; set; } = EquipmentBrandEnum.AIOI;
        /// <summary>
        /// 标签类型
        /// </summary>
        public EquipmentLabelTypeEnum LabelType { get; set; } = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT;
        /// <summary>
        /// 地址列表
        /// </summary>
        public List<string> AddressList { get; set; }

        public CancelPacketModel() { CmdType = CommunicationCmdTypeEnum.CANCEL; }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte> { (byte)EquipmentBrand, (byte)LabelType };
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //// 端口
            //result.AddRange(BitConverter.GetBytes(Port));
            // 地址列表
            AddressList.ForEach(p =>
            {
                result.AddRange(ByteUtils.StringToUtf8ByteArray(p));
            });

            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            //int len;
            string addr;
            EquipmentBrand = (EquipmentBrandEnum)iData[iStartPosition++];
            LabelType = (EquipmentLabelTypeEnum)iData[iStartPosition++];
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// ip地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            // 地址列表
            AddressList = new List<string>();
            while (iStartPosition + PACKET_TAIL_LENGTH < iData.Length)
            {
                addr = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
                if (!string.IsNullOrEmpty(addr)) AddressList.Add(addr);
            }
        }
    }

    /// <summary>
    /// 动作数据包模型,Client->Service
    /// </summary>
    public class ActionPacketModel : ServerPacketModel
    {
        /// <summary>
        /// 设备厂家
        /// </summary>
        public EquipmentBrandEnum EquipmentBrand { get; set; } = EquipmentBrandEnum.AIOI;
        /// <summary>
        /// 标签类型
        /// </summary>
        public EquipmentLabelTypeEnum LabelType { get; set; } = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT;
        /// <summary>
        /// 标签地址列表
        /// </summary>
        public List<string> AddressList { get; set; }
        /// <summary>
        /// 控制模式
        /// </summary>
        public DeviceActionModel ControlModel { get; set; }

        public ActionPacketModel() { CmdType = CommunicationCmdTypeEnum.ACTION; }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte> { (byte)EquipmentBrand };// 厂家标识
            result.Add((byte)LabelType);// 设备类型
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            // IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //result.AddRange(BitConverter.GetBytes(Port));

            result.Add((byte)AddressList.Count);// 设备个数
            // 设备地址
            foreach (string item in AddressList)
            {
                result.AddRange(ByteUtils.StringToUtf8ByteArray(item));
            }
            result.Add((byte)ControlModel.LightModel);// 亮灯模式
            result.Add((byte)ControlModel.OpenBuzzer);// 蜂鸣器状态
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControlModel.Seq));// 显示字符
            result.Add((byte)(ControlModel.IsScroll ? 0x01 : 0x00));// 显示字符是否滚动,MWU2414专用

            result.Add((byte)(ControlModel.IsUsedConfirm ? 0x01 : 0x00));// 是否使用Confrim或感应手环
            result.Add((byte)ControlModel.ConfirmLightModel);// 按Confrim键后的亮灯模式
            result.Add((byte)ControlModel.ConfirmOpenBuzzer);// 按Confrim键后的蜂鸣器状态
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControlModel.ConfirmSeq));// 确认显示字符
            result.Add((byte)(ControlModel.ConfirmIsScroll ? 0x01 : 0x00));// 按Confrim键后的显示字符是否滚动,MWU2414专用

            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControlModel.UhfRfId));// UHF-RF-ID
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControlModel.EpcMark));// EPC Mark掩码

            logObj.Info($"序列化发送信息:地址:{AddressList[0]}\t显示内容:{ControlModel.Seq}");

            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            int count;
            EquipmentBrand = (EquipmentBrandEnum)iData[iStartPosition++];// 厂家标识
            LabelType = (EquipmentLabelTypeEnum)iData[iStartPosition++];// 设备类型
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            // 数量
            count = iData[iStartPosition++];
            AddressList = new List<string>();
            for (int i = 0; i < count; i++)
            {
                AddressList.Add(ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition));
            }
            ControlModel = new DeviceActionModel();
            ControlModel.LightModel = (CommCmdLightModeEnum)iData[iStartPosition++];// 亮灯模式
            ControlModel.OpenBuzzer = (DeviceBuzzerStatuEnum)iData[iStartPosition++];// 蜂鸣器状态
            ControlModel.Seq = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            ControlModel.IsScroll = iData[iStartPosition++] == 0x01;// 显示字符是否滚动,MWU2414专用
            ControlModel.IsUsedConfirm = iData[iStartPosition++] == 0x01;// 是否使用Confrim或感应手环
            ControlModel.ConfirmLightModel = (CommCmdLightModeEnum)iData[iStartPosition++];// 按Confirmr后的亮灯模式
            ControlModel.ConfirmOpenBuzzer = (DeviceBuzzerStatuEnum)iData[iStartPosition++];// 按Confirmr后的蜂鸣器状态
            ControlModel.ConfirmSeq = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);// 按Confirmr后的显示字符
            ControlModel.ConfirmIsScroll = iData[iStartPosition++] == 0x01;// 按Confirmr后显示字符是否滚动,MWU2414专用
            ControlModel.UhfRfId = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);// UHF-RF-ID
            ControlModel.EpcMark = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);// EPC Mark掩码
        }
    }

    /// <summary>
    /// 获取按钮状态数据包模型,Client->Service
    /// </summary>
    public class GetLabelStatePacketModel : ServerPacketModel
    {
        /// <summary>
        /// 设备厂家
        /// </summary>
        public EquipmentBrandEnum EquipmentBrand { get; set; } = EquipmentBrandEnum.AIOI;
        /// <summary>
        /// 标签类型
        /// </summary>
        public EquipmentLabelTypeEnum LabelType { get; set; } = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT;
        /// <summary>
        /// 标签地址
        /// </summary>
        public string Address { get; set; }

        public GetLabelStatePacketModel() { CmdType = CommunicationCmdTypeEnum.GET_LABEL_STATE; }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte> { (byte)EquipmentBrand };// 设备厂家
            result.Add((byte)LabelType);// 标签类型
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //result.AddRange(BitConverter.GetBytes(Port));// 端口

            result.AddRange(ByteUtils.StringToUtf8ByteArray(Address));// 地址
            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            EquipmentBrand = (EquipmentBrandEnum)iData[iStartPosition++];
            LabelType = (EquipmentLabelTypeEnum)iData[iStartPosition++];
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);

            Address = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
        }
    }

    /// <summary>
    /// 触发标签数据包模型,Service->Client
    /// </summary>
    public class TriggerLabelPacketModel : ServerPacketModel
    {
        /// <summary>
        /// 标签地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 设备状态
        /// </summary>
        public DeviceStateEnum State { get; set; }
        /// <summary>
        /// 显示数据
        /// </summary>
        public string DisplayData { get; set; }
        /// <summary>
        /// 模式状态;
        /// </summary>
        public LabelModelStateEnum ModuleState { get; set; }
        /// <summary>
        /// UHF RF-ID 
        /// </summary>
        public string EpcData { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }

        public TriggerLabelPacketModel()
        {
            CmdType = CommunicationCmdTypeEnum.RETURN_SINGLE;
        }

        public override string ToString()
        {
            return $"触发标签!地址:{Address} 设备状态:{State} 显示数据:{DisplayData} 模式状态:{ModuleState} RF-ID:{EpcData} Message:{Message}";
        }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //// 端口
            //result.AddRange(BitConverter.GetBytes(Port));
            // 地址
            result.AddRange(ByteUtils.StringToUtf8ByteArray(Address));
            // 设备状态
            result.Add((byte)State);
            // 显示数据
            result.AddRange(ByteUtils.StringToUtf8ByteArray(DisplayData));
            // Module 状态
            result.Add((byte)ModuleState);
            // UHF RF-ID
            result.AddRange(ByteUtils.StringToUtf8ByteArray(EpcData));
            // 消息
            result.AddRange(ByteUtils.StringToUtf8ByteArray(Message));

            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// IP和端口
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            // 地址
            Address = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            // 设备状态
            State = (DeviceStateEnum)iData[iStartPosition++];
            // 显示数据
            DisplayData = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            // Module状态
            ModuleState = (LabelModelStateEnum)iData[iStartPosition++];
            // UHF RF-ID
            EpcData = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            // 消息
            Message = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
        }
    }

    /// <summary>
    /// 异常信息的数据包模型,Service->Client
    /// </summary>
    public class ExceptionInfoPacketModel : ServerPacketModel
    {
        /// <summary>
        /// 异常代码
        /// </summary>
        public ControllerExceptionCodeEnum ExceptionCode { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        public ExceptionInfoPacketModel()
        {
            CmdType = CommunicationCmdTypeEnum.RETURN_EXCEPTION;
        }

        public override string ToString()
        {
            return $"设备异常!地址:{Address} 异常代码:{ExceptionCode}";
        }

        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //// 端口
            //result.AddRange(BitConverter.GetBytes(Port));
            // 异常代码
            result.Add((byte)ExceptionCode);
            // 地址
            result.AddRange(ByteUtils.StringToUtf8ByteArray(Address));

            return result.ToArray();
        }

        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            // 异常代码
            ExceptionCode = (ControllerExceptionCodeEnum)iData[iStartPosition++];
            // 地址
            Address = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
        }
    }

    /// <summary>
    /// 按钮状态,Service->Client
    /// </summary>
    public class LabelStatePacketModel : ServerPacketModel
    {
        /// <summary>
        /// 标签地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 标签状态
        /// </summary>
        public EquipmentRunStateEnum LabelStateType { get; set; }
        /// <summary>
        /// 按键+是否按下
        /// </summary>
        public bool KeyIncrease { get; set; }
        /// <summary>
        /// 按键-是否按下
        /// </summary>
        public bool KeyDecrease { get; set; }
        /// <summary>
        /// 按键Fn是否按下
        /// </summary>
        public bool KeyFn { get; set; }
        /// <summary>
        /// 是否按下按钮
        /// </summary>
        public bool KeyConfirm { get; set; }

        public LabelStatePacketModel() { CmdType = CommunicationCmdTypeEnum.RETURN_LABEL_STATE; }

        public override string ToString()
        {
            return $"按钮状态!地址:{Address} 标签状态:{LabelStateType}";
        }

        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //// 端口
            //result.AddRange(BitConverter.GetBytes(Port));
            // 地址
            result.AddRange(ByteUtils.StringToUtf8ByteArray(Address));
            // 标签状态
            result.Add((byte)LabelStateType);
            result.Add((byte)(KeyIncrease ? 0x01 : 0x00));// 按键+是否按下
            result.Add((byte)(KeyDecrease ? 0x01 : 0x00));// 按键-是否按下
            result.Add((byte)(KeyFn ? 0x01 : 0x00));// 按键Fn是否按下
            result.Add((byte)(KeyConfirm ? 0x01 : 0x00));// 是否按下按钮

            return result.ToArray();
        }

        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {

            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //Port = BitConverter.ToInt32(iData, iStartPosition);// 端口
            //iStartPosition += sizeof(int);
            Address = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);// 地址
            LabelStateType = (EquipmentRunStateEnum)iData[iStartPosition++];// 标签状态
            KeyIncrease = iData[iStartPosition++] == 0x01;// 按键+是否按下
            KeyDecrease = iData[iStartPosition++] == 0x01;// 按键-是否按下
            KeyFn = iData[iStartPosition++] == 0x01;// 按键Fn是否按下
            KeyConfirm = iData[iStartPosition++] == 0x01; // 是否按下按钮
        }
    }

    /// <summary>
    /// 控制器状态
    /// </summary>
    public class ControllerStatePacketModel : ServerPacketModel
    {
        /// <summary>
        /// 控制器状态
        /// </summary>
        public ControllerStateEnum State { get; set; }

        public ControllerStatePacketModel() { CmdType = CommunicationCmdTypeEnum.CONTROLLER_STATE; }

        public override string ToString()
        {
            //return $"控制器[{IpAddress}:{Port}]状态:{State}";
            return $"控制器[{ControllerKey}]状态:{State}";
        }

        public override byte[] BodyData() {
            List<byte> result = new List<byte>();
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //// 端口
            //result.AddRange(BitConverter.GetBytes(Port));
            // 控制器状态
            result.Add((byte)State);
            return result.ToArray();
        }

        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            // IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            State = (ControllerStateEnum)iData[iStartPosition];
            iStartPosition++;
        }
    }

    /// <summary>
    /// 注册设备
    /// </summary>
    public class RegistrationDevicePacketModel : ServerPacketModel
    {
        /// <summary>
        /// 地址列表
        /// </summary>
        public string[] AddressList { get; set; }

        public RegistrationDevicePacketModel() { CmdType = CommunicationCmdTypeEnum.REGISTRATION_DEVICE; }

        public override string ToString()
        {
            return $"控制器[{ControllerKey}]注册设备!";
        }

        public override byte[] BodyData() {
            List<byte> result = new List<byte>();
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //// 端口
            //result.AddRange(BitConverter.GetBytes(Port));
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            if (AddressList == null) AddressList = new string[0];
            result.Add((byte)AddressList.Length);
            foreach (string item in AddressList) {
                result.AddRange(ByteUtils.StringToUtf8ByteArray(item));
            }
            return result.ToArray();
        }

        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            AddressList = new string[iData[iStartPosition]];
            iStartPosition++;
            for (int i = 0; i < AddressList.Length; i++) {
                AddressList[i] = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            }
        }
    }

    /// <summary>
    /// 获取设备电压
    /// </summary>
    public class GetDeviceVoltagePacketModel : ServerPacketModel {
        /// <summary>
        /// 设备厂家
        /// </summary>
        public EquipmentBrandEnum EquipmentBrand { get; set; } = EquipmentBrandEnum.AIOI;
        /// <summary>
        /// 标签类型
        /// </summary>
        public EquipmentLabelTypeEnum LabelType { get; set; } = EquipmentLabelTypeEnum.AIOI_LABEL_NW2991;
        /// <summary>
        /// 标签地址
        /// </summary>
        public string Address { get; set; }

        public GetDeviceVoltagePacketModel() { CmdType = CommunicationCmdTypeEnum.GET_LABEL_VOLTAGE; }

        public override string ToString()
        {
            return "获取设备电压";
        }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte> { (byte)EquipmentBrand };// 设备厂家
            result.Add((byte)LabelType);// 标签类型
            /// 控制器的键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            //// IP地址
            //result.AddRange(ByteUtils.StringToUtf8ByteArray(IpAddress));
            //result.AddRange(BitConverter.GetBytes(Port));// 端口

            result.AddRange(ByteUtils.StringToUtf8ByteArray(Address));// 地址
            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            EquipmentBrand = (EquipmentBrandEnum)iData[iStartPosition++];
            LabelType = (EquipmentLabelTypeEnum)iData[iStartPosition++];
            //// IP地址
            //IpAddress = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            //// 端口
            //Port = BitConverter.ToInt32(iData, iStartPosition);
            //iStartPosition += sizeof(int);
            // 控制器主键
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);

            Address = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
        }
    }

    /// <summary>
    /// 显示数据
    /// </summary>
    public class DisplayDataPacketModel : ServerPacketModel {
        /// <summary>
        /// X轴位置
        /// </summary>
        public byte PositionX { get; set; } = 1;
        /// <summary>
        /// Y轴位置
        /// </summary>
        public byte PositionY { get; set; } = 1;
        /// <summary>
        /// 字体大小
        /// </summary>
        public byte FontSize { get; set; } = 32;
        /// <summary>
        /// 显示数据
        /// </summary>
        public string DisplayData { get; set; }

        public DisplayDataPacketModel() { CmdType = CommunicationCmdTypeEnum.DISPLAY_DATA; }

        public override string ToString()
        {
            return "显示数据";
        }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            // 控制器键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            result.Add(PositionX);
            result.Add(PositionY);
            result.Add(FontSize);
            // 显示数据
            result.AddRange(ByteUtils.StringToUtf8ByteArray(DisplayData));
            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            PositionX = iData[iStartPosition++];
            PositionY = iData[iStartPosition++];
            FontSize = iData[iStartPosition++];
            DisplayData = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
        }

    }

    /// <summary>
    /// 振动信号
    /// </summary>
    public class VibrationPacketModel : ServerPacketModel
    {
        /// <summary>
        /// 是否振动
        /// </summary>
        public bool IsVibration { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public VibrationPacketModel() { CmdType = CommunicationCmdTypeEnum.VIBRATION; }

        public override string ToString()
        {
            return "振动信号";
        }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));// 控制器键
            result.Add((byte)(IsVibration ? 0x01 : 0x00));// 是否振动
            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体数据
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            IsVibration = iData[iStartPosition] == 0x01;
        }
    }

    /// <summary>
    /// 写RFID
    /// </summary>
    public class WriteRfidPacketModel : ServerPacketModel { 
        /// <summary>
        /// RFID
        /// </summary>
        public uint RFID { get; set; }

        public WriteRfidPacketModel()
        {
            CmdType = CommunicationCmdTypeEnum.WRITE_RFID;
        }

        public override string ToString()
        {
            return "写RFID";
        }

        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            // 控制器键
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            result.AddRange(BitConverter.GetBytes(RFID));
            return result.ToArray();
        }

        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            RFID = BitConverter.ToUInt32(iData, iStartPosition);
        }
    }

    /// <summary>
    /// 设置PanID和信道
    /// </summary>
    public class SettingPanIdChannelPacketModel: ServerPacketModel
    {
        /// <summary>
        /// 模块PAN ID
        /// </summary>
        public byte PanId { get; set; }
        /// <summary>
        /// 信道
        /// </summary>
        public byte ChannelNumber { get; set; }
        public byte DutyNumber { get; set; }

        public SettingPanIdChannelPacketModel() { CmdType = CommunicationCmdTypeEnum.SETTING_PANID_CHANNEL; }

        public override string ToString()
        {
            return "设置PAN ID和信道";
        }

        /// <summary>
        /// 获取数据体
        /// </summary>
        /// <returns></returns>
        public override byte[] BodyData()
        {
            List<byte> result = new List<byte>();
            result.AddRange(ByteUtils.StringToUtf8ByteArray(ControllerKey));
            result.Add(PanId);
            result.Add(ChannelNumber);
            result.Add(DutyNumber);
            return result.ToArray();
        }

        /// <summary>
        /// 解析数据体
        /// </summary>
        /// <param name="iData"></param>
        /// <param name="iStartPosition"></param>
        public override void ParseBodyData(byte[] iData, int iStartPosition = 0)
        {
            ControllerKey = ByteUtils.ByteArrayToUtf8String(iData, ref iStartPosition);
            PanId = iData[iStartPosition++];
            ChannelNumber = iData[iStartPosition++];
            DutyNumber = iData[iStartPosition++];
        }
    }
}
